package com.bjsj.pro.base.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjsj.pro.R;


/**
 * 网络请求的布局
 * */
@SuppressLint("InflateParams")
public class NetworkLayout extends RelativeLayout {
    private View rootView;
    private TextView mRestart;
    private TextView network_tv;
    private TextView tv_text;
    private ImageView image_ty;
    private LinearLayout jiazai;
    private LinearLayout emptylays;
    private LinearLayout network_no;
    private NoNetWorkLayoutCallback callback;
    public void setOnReloadListeners(NoNetWorkLayoutCallback listener) {
        this.callback = listener;
    }
    public NetworkLayout(Context context) {
        this(context, null);
    }
    public NetworkLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }
    public NetworkLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
        this.findViews();
    }
    private void init() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.failnetworkd, null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(rootView,params);
    }
    private void findViews() {
        jiazai = (LinearLayout) findViewById(R.id.jiazai);
        emptylays = (LinearLayout) findViewById(R.id.emptylays);
        network_no = (LinearLayout) findViewById(R.id.network_no);
        mRestart = (TextView)findViewById(R.id.network_layout_restart);
        network_tv = (TextView)findViewById(R.id.network_tv);
        tv_text = (TextView)findViewById(R.id.tv_text);
        image_ty = (ImageView) findViewById(R.id.image_ty);
        mRestart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(callback!=null){
                    callback.reloadData();
                }
            }
        });
    }

    public  void  setNetworkErroMsg(String text){
        network_tv.setText(text);

    }


    public  void  setNetworkImage(int image){
        image_ty.setImageResource(image);
    }

    //网络出错
    public  void  setServerError(){

    }
    public void setNonetWorkVisible(){//请求网络失败
        jiazai.setVisibility(View.GONE);
        emptylays.setVisibility(View.GONE);
        network_no.setVisibility(View.VISIBLE);
        setVisibility(View.VISIBLE);
//        rootView.setVisibility(View.VISIBLE);
    }
    public void setHasGetMsgVisible(){//请求网络成功
//        rootView.setVisibility(View.GONE);
        setVisibility(View.GONE);
    }
    public void setAgainGetMsg(){//重新请求网络（进来初始设置）
        jiazai.setVisibility(View.VISIBLE);
        network_no.setVisibility(View.GONE);
        emptylays.setVisibility(View.GONE);
//        rootView.setVisibility(View.VISIBLE);
        setVisibility(View.VISIBLE);
    }

    public void setEmtyLayout(){//空布局
        jiazai.setVisibility(View.GONE);
        network_no.setVisibility(View.GONE);
        setVisibility(View.VISIBLE);
//        rootView.setVisibility(View.VISIBLE);
        emptylays.setVisibility(View.VISIBLE);
        tv_text.setVisibility(View.VISIBLE);
        image_ty.setVisibility(View.VISIBLE);
    }
    public void setCompeletEmtyLayout(){//完全空布局
        jiazai.setVisibility(View.GONE);
        network_no.setVisibility(View.GONE);

        setVisibility(View.VISIBLE);
//        rootView.setVisibility(View.VISIBLE);
        emptylays.setVisibility(View.VISIBLE);
        tv_text.setVisibility(View.GONE);
        image_ty.setVisibility(View.GONE);
    }
    public interface NoNetWorkLayoutCallback{
        public void reloadData();
    }
}
